package com.itheima.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.common.BaseContext;
import com.itheima.pojo.Employee;
import com.itheima.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * reggie-com.itheima.config
 * Administrator
 * 2023-03-07
 * 14:33
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //吃方法在同一个浏览器登录不同用户 无法识别session
    //@Autowired
    //private  HttpSession session;

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("insert 填充");
        //if (session!=null){
        //    Employee emp = (Employee) session.getAttribute("emp");
        //    Long id = emp.getId();
            //其中方法参数中第一个是前面自动填充所对应的字段，第二个是要自动填充的值\
            this.setFieldValByName("createTime",LocalDateTime.now(),metaObject);
            this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
            //this.setFieldValByName("createUser",new Long(id),metaObject);
            //this.setFieldValByName("updateUser",new Long(id),metaObject);m666
            this.setFieldValByName("createUser", BaseContext.getCurrentId(),metaObject);
            this.setFieldValByName("updateUser",BaseContext.getCurrentId(),metaObject);
            this.setFieldValByName("password",new String(MD5Util.string2MD5("123456")),metaObject);
        //}


    }


    @Override
    public void updateFill(MetaObject metaObject) {
        //if (session!=null){
        //    Employee emp = (Employee) session.getAttribute("emp");
        //    Long id = emp.getId();
            this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
            //this.setFieldValByName("updateUser",new Long(id),metaObject);
            this.setFieldValByName("updateUser",BaseContext.getCurrentId(),metaObject);
        //}

    }
}
